File Handling (Script)

 

Often there is a need to open an existing file or write information to a new file. To achieve this you can use the calls as described in the examples below.

 

Open a file for reading

var handle = OpenFileForReading("C:\\Users\\administrator\\Documents\\Sym3 Integrator\\ChutesMap.csv");
var line = ReadLine(handle);
// ...
CloseFile(handle);

Open a file for writing

var handle = OpenFileForWriting("C:\\Users\\administrator\\Documents\\Sym3 Integrator\\ChutesMap.csv");
// Boolean type: Flush file after writing the line. Default = false (for compatibility)
WriteLine(handle,"ABS01",true);
// ...
CloseFile(handle);

Handle Seek(fileHandle: handle, offset: integer, whence: integer (OPTIONAL));

 

This function sets the current position of this fileHandle to the given offset according to whence which is optional.

By default whence is 0.

 

Name Type Description
fileHandle Handle The handle of the file which to be associate (the handle is returned by OpenFileForReading)
offset Integer Offset value to set the current position
whence Integer

Position used as reference for the offset. It is specified by one of the following to be used as arguments for this function:

0 - Seek from beginning of file (default)

1 - Seek from current position of file

2 - Seek from end of file

Return value - None

string GetProjectDirectory();

 

New in v7.19 - This function returns the directory where the current project is located. Will returns an empty string if the project has not been saved yet.

 

Return value - a string that contains the directory where the current project is located. The directory will end with a folder seprator. Example: C:\Temp\

Example:

var dir = GetProjectDirectory();
var handle = OpenFileForReading(dir + "ChutesMap.csv");

Example:

function OnSimulationStart()
{
	// Open file for reading
	var handle = OpenFileForReading("C:\\Users\\administrator\\Documents\\Sym3 Integrator\\ChutesMap.csv");
	
	//Seek from beginning of file
	Seek(handle,200000,0);
	var _line = ReadLine(handle);
	LogDebug("Seek from beginning of file: " + _line);
	
	//Seek from current position of file
	Seek(handle,299999,1);
	var _line1 = ReadLine(handle);
	LogDebug("Seek from current position of file: " + _line1);
	
	
	//Seek from end of file
	Seek(handle,200000,2);
	var _line2 = ReadLine(handle);
	LogDebug("Seek from end of file: " + _line2);
	
	CloseFile(handle);
}

 

Sample script to load a CSV file into memory array and address as cells